package com.czh.subject.infra.rpc;

import com.czh.auth.api.feign.UserFeignService;
import com.czh.auth.api.entity.AuthUserDTO;
import com.czh.auth.api.entity.ResponseResult;
import com.czh.subject.infra.rpc.entity.UserInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author caozaihui
 * @version 1.0
 * @since 2024/9/8 13:57
 */
@Component
public class UserRPC {

    @Resource
    private UserFeignService userFeignService;

    public UserInfo getUserInfo(String userName) {
        AuthUserDTO authUserDTO = new AuthUserDTO();
        authUserDTO.setUserName(userName);
        ResponseResult<AuthUserDTO> result = userFeignService.getUserInfo(authUserDTO);
        UserInfo userInfo = new UserInfo();
        if (result.getSuccess()) {
            AuthUserDTO userDTO = result.getData();
            userInfo.setUsername(userDTO.getUserName());
            userInfo.setNickname(userDTO.getNickName());
        }
        return userInfo;
    }

    public List<UserInfo> getUserInfoList(List<String> userNameList) {
        ResponseResult<List<AuthUserDTO>> userInfoList = userFeignService.getUserInfoList(userNameList);
        LinkedList<UserInfo> userInfos = new LinkedList<>();
        if (userInfoList.getSuccess() && !userInfoList.getData().isEmpty()) {
            userInfoList.getData().forEach(userInfo -> userInfos.add(new UserInfo().setNickname(userInfo.getNickName()).setAvatar(userInfo.getAvatar()).setUsername(userInfo.getUserName())));
            return userInfos;
        }
        return Collections.emptyList();
    }
}
